package com.bky.jdbc;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shipeilei on 2017/5/26.
 */
public class IResultSetExtractor implements ResultSetExtractor<List<Map<String, Object>>>{
    @Override
    public List<Map<String, Object>> extractData(ResultSet rs) throws SQLException, DataAccessException {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        while (rs.next()){
            Map<String, Object> map = new HashMap<String,Object>();
            ResultSetMetaData metaData = rs.getMetaData();
            for (int i=1; i<=metaData.getColumnCount(); i++){
                map.put(metaData.getColumnName(i), rs.getObject(i));
            }
            list.add(map);
        }
        return list;
    }
}
